package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class BookAppealActivity extends BaseActivity implements View.OnClickListener{
    /**
     * 申诉
     */
    private ImageView appealback;
    private EditText inmessage;
    private Button submintbt;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String id;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_appeal);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        initAppealView();
    }

    private void initAppealView() {
        appealback = (ImageView) findViewById(R.id.book_appeal_back);
        inmessage = (EditText) findViewById(R.id.appeal_inmessage);
        submintbt = (Button) findViewById(R.id.appeal_submitbt);
        backmain = (ImageView) findViewById(R.id.bookappeal_backmain);

        backmain.setOnClickListener(this);
        appealback.setOnClickListener(this);
        submintbt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.book_appeal_back:
                finish();
                break;

            case R.id.appeal_submitbt:
                submintMessage();
                break;
            case R.id.bookappeal_backmain:
                startActivity(new Intent(BookAppealActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
        }
    }
        // 提交投诉信息
    private void submintMessage() {
        String message = String.valueOf(inmessage.getText());
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.BOOKAPPEAL);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject object = new JSONObject();
        object.put("orderId",id);
        object.put("content",message);
        params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject object1 = JSON.parseObject(result);
                String status = object1.getString("status");
                if (status.equals("1")){
                    Toast.makeText(BookAppealActivity.this, "申诉成功", Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    Toast.makeText(BookAppealActivity.this, "已申诉", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(BookAppealActivity.this,ex);            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
